package edu.gatech.gro.model.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import edu.gatech.gro.model.GroceryStore;

public class GroceryStoreDao extends DatabaseHandler {

	private static final String TAG = "GROCERY_STORE_DAO";

	public GroceryStoreDao(Context context) {
		super(context);
	}

	@Override
	public void clear() {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(GROCERY_STORE_TABLE, "id > 0", null);
		db.close();
	}

	public ArrayList<GroceryStore> getAllGroceryStores() {
		ArrayList<GroceryStore> list = new ArrayList<GroceryStore>();
		String selectQuery = "SELECT  * FROM " + GROCERY_STORE_TABLE;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor.moveToFirst()) {
			do {
				GroceryStore store = new GroceryStore();
				store.setId(cursor.getInt(0));
				store.setName(cursor.getString(1));
				store.setNameClean(cursor.getString(2));
				store.setCreationTime(cursor.getInt(3));
				store.setLastUpdateTime(cursor.getInt(4));
				store.setDeleteFlag(cursor.getInt(5) == 1);
				list.add(store);
			} while (cursor.moveToNext());
		}

		db.close();
		return list;
	}

	public int count() {
		String countQuery = "SELECT  * FROM " + GROCERY_STORE_TABLE;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();
		db.close();
		return cursor.getCount();
	}

	public boolean saveGroceryStore(final GroceryStore store) {
		if (store == null) {
			return false;
		}

		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("name", store.getName());
		values.put("nameClean", store.getNameClean());
		values.put("creationTime", store.getCreationTime());
		values.put("lastUpdateTime", store.getLastUpdateTime());
		values.put("deleteFlag", store.isDeleteFlag() ? 1 : 0);

		long id = db.insert(GROCERY_STORE_TABLE, null, values);
		if (id == -1) {
			// TODO: error
			return false;
		} else {
			store.setId((int) id);
		}

		db.close();
		return true;
	}

	public boolean upgrade(ArrayList<GroceryStore> stores) {
		if (stores == null) {
			return false;
		}

		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		for (GroceryStore store : stores) {
			if (store.isDeleteFlag()) {
				db.delete(GROCERY_STORE_TABLE, "id = ?", new String[] { String.valueOf(store.getId()) });
			} else {
				values.clear();
				values.put("id", store.getId());
				values.put("name", store.getName());
				values.put("nameClean", store.getNameClean());
				values.put("creationTime", store.getCreationTime());
				values.put("lastUpdateTime", store.getLastUpdateTime());
				values.put("deleteFlag", 0);
				db.replace(GROCERY_STORE_TABLE, null, values);
			}
		}
		db.close();

		return true;
	}

	public boolean updateGroceryStore(final GroceryStore store) {
		if (store == null || store.getId() < 1) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("name", store.getName());
		values.put("nameClean", store.getNameClean());
		values.put("lastUpdateTime", store.getLastUpdateTime());
		values.put("deleteFlag", store.isDeleteFlag() ? 1 : 0);

		// updating row
		int r = db.update(GROCERY_STORE_TABLE, values, "id = ?", new String[] { String.valueOf(store.getId()) });
		db.close();
		return r == 1;
	}

	public boolean deleteGroceryStore(final GroceryStore store) {
		if (store == null || store.getId() < 1) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();
		int r = db.delete(GROCERY_STORE_TABLE, "id = ?", new String[] { String.valueOf(store.getId()) });
		db.close();
		return r == 1;
	}
}
